$(document).ready(function(){

	loadKartu();
	$("#btn_simpan").click(function(data){
		$.post('controller/cont.kartu.php',$("#frm_kartu").serialize(),function(data){
			if(data.msg){
				if(data.edit){
					goAdd();
					document.getElementById('nip_peg').value=data.kartu.nip;
                    document.getElementById('nomor_kartu').value=data.kartu.nomorKartu;
                    document.getElementById("frm_aksi").value = "simpan_edit";
                    document.getElementById("detail_pegawai").style.display = "block";
                    document.getElementById("nip_peg").focus();
				}else{
					document.getElementById("sukses").innerHTML = data.sukses;
                    $("#div_sukses").fadeIn(500).delay(3500).fadeOut(500);
                    loadKartu();
                    goUndo();
				}
			}else{
				document.getElementById("error").innerHTML = data.eror;
                $("#div_error").fadeIn(500).delay(3500).fadeOut(500);
                goUndo();
			}
		},'json');
		return false;
	});


	$("#nip_peg").keyup(function() {
        var val=$('#nip_peg').val();
        //if(val.length===18){
            $.getJSON('controller/cont.pegawai.php',{aksi_biodata:true,nip:val},function(data){
                if(data.msg){
                    document.getElementById('nip_detail').innerHTML=data.nip;
                    document.getElementById('nama_detail').innerHTML=data.nama;
                    document.getElementById('gol_detail').innerHTML=data.pangkat+" / "+data.golongan;
                    document.getElementById('jabatan_detail').innerHTML=data.eselon.split("\n")[0];
                    document.getElementById("detail_pegawai").style.display = "block";
                }else{
                   	document.getElementById('nip_detail').innerHTML="Data Tidak Ada";
                    document.getElementById('nama_detail').innerHTML="Data Tidak Ada";
                    document.getElementById('gol_detail').innerHTML="Data Tidak Ada";
                    document.getElementById('jabatan_detail').innerHTML="Data Tidak Ada";
                    document.getElementById("detail_pegawai").style.display = "block";
                }
                
            });
        //}   
    });
});

function loadKartu(){
	$.getJSON('controller/cont.kartu.php',{aksi_get_kartu:true},function(data){
		$('#list_kartu tbody').html('');
        var tab = '';
        if (data.msg) {
            $.each(data.kartu, function(index, kartu) {
            	if(kartu.kartu===false){
            		tab += "<tr>";
	                tab += "<td>" + kartu.pegawai.nip + "</td>";
	                tab += "<td>" + kartu.pegawai.nama + "</td>";
	                tab += "<td colspan=\"2\"> Belum Memiliki Kartu Cuti</td>";
	                //tab += "<td align=\"center\"><input id=\"selectLibur\" name=\"selectLibur\" type=\"radio\" value=\"" + libur.id + "\"/></td>";
	                tab += "</tr>";
            	}else{
            		tab += "<tr>";
	                tab += "<td>" + kartu.pegawai.nip + "</td>";
	                tab += "<td>" + kartu.pegawai.nama + "</td>";
	                tab += "<td>" + kartu.kartu.nomorKartu + "</td>";
	                tab += "<td align=\"center\"><input id=\"selectLibur\" name=\"selectLibur\" type=\"radio\" value=\"" + kartu.kartu.id + "\"/></td>";
	                tab += "</tr>";
            	}
            });

        } else {
            tab += "<tr>"
            tab += "<td colspan=\"5\">Data Tidak Ada</td>";
            tab += "</tr>";
        }
        $('#list_kartu tbody').append(tab);
	});
}